function [Tabout, P5slop, P5sig, P5rsq,Coeff_Pval_Int] = Runregsdailyf(pric,yiel,dpric,dyiel,sambeg,samend)
%Regression for Low and High yields DAILY data
% 1) run for 33% and 50% cutoffs
% 2) for 5 portfolios low to high
% 3) regression with interaction term

% Select samples 
pric  = pric(dpric>=sambeg &dpric<=samend );
yiel  = yiel(dyiel>=sambeg &dyiel<=samend );

Dpric = log(pric(2:end)./ pric(1:end-1));
Dyiel = log((1+yiel(2:end)/100)./(1+yiel(1:end-1)/100));

%%%%%%%%%%%% SPLIT SAMPLE in 2 
% **** 33 cutoff Create low and high rate samples
cuto = 33;
sampfilL = yiel(2:end)<=prctile(yiel(2:end),cuto);    
sampfilH = yiel(2:end)>prctile(yiel(2:end),cuto); 

%Run regressions
stats = regstats2( Dpric(sampfilL), Dyiel(sampfilL),'linear');  beta1=stats.beta; pval1=stats.hac.pval; rsq1=stats.adjrsquare; 
stats = regstats2( Dpric(sampfilH), Dyiel(sampfilH),'linear');  beta2=stats.beta; pval2=stats.hac.pval; rsq2=stats.adjrsquare; 
stats = regstats2( Dpric, Dyiel,'linear');                      beta3=stats.beta; pval3=stats.hac.pval; rsq3=stats.adjrsquare; 

YL  = [beta1(2); rsq1];
YLp{1} = staf(pval1(2));
YLp{2} = ' ';
YH  = [beta2(2); rsq2];
YHp{1} = staf(pval2(2));
YHp{2} = ' ';
Y   = [beta3(2); rsq3];
Yp{1} = staf(pval3(2));
Yp{2} = ' ';

% **** 50 cutoff Create low and high rate samples
cuto = 50;
sampfilL = yiel(2:end)<=prctile(yiel(2:end),cuto);    
sampfilH = yiel(2:end)>prctile(yiel(2:end),cuto); 

%Run regressions
stats = regstats2( Dpric(sampfilL), Dyiel(sampfilL),'linear');  beta1=stats.beta; pval1=stats.hac.pval; rsq1=stats.adjrsquare; 
stats = regstats2( Dpric(sampfilH), Dyiel(sampfilH),'linear');  beta2=stats.beta; pval2=stats.hac.pval; rsq2=stats.adjrsquare; 
stats = regstats2( Dpric, Dyiel,'linear');                      beta3=stats.beta; pval3=stats.hac.pval; rsq3=stats.adjrsquare; 

YL  = [YL;  beta1(2); rsq1];
YLp{3} = staf(pval1(2));
YLp{4} = ' ';
YH  = [YH;  beta2(2); rsq2];
YHp{3} = staf(pval2(2));
YHp{4} = ' ';
Y   = [Y;   beta3(2); rsq3];
Yp{3} = staf(pval3(2));
Yp{4} = ' ';

uplo=0;
if uplo==1
subplot(2,1,1)
plot(Dpric(sampfilL), Dyiel(sampfilL),'.')
title('low yields')
axis([-.1 .1 -.007 .004])
subplot(2,1,2)
plot(Dpric(sampfilH), Dyiel(sampfilH),'.')
title('high yields')
axis([-.1 .1 -.007 .004])
end    %uplo

TStat = {'33% cutof: slope';'Rsquare'; '50% cutof: slope';'Rsquare' };
Tabout = table(TStat,YL,YLp',YH,YHp',Y,Yp');


%%% Split sample in 5 portfolios
sortvar =yiel(2:end);       % with end or starting period yiel(2:end) or yiel(1:end-1)

y = quantile(sortvar,[.2 .4 .6 .8 ]);
sampfil1 = sortvar<=y(1); 
sampfil2 = y(1)<sortvar&sortvar<=y(2); 
sampfil3 = y(2)<sortvar&sortvar<=y(3); 
sampfil4 = y(3)<sortvar&sortvar<=y(4); 
sampfil5 = y(4)<sortvar; 

stats = regstats2( Dpric(sampfil1), Dyiel(sampfil1),'linear');  beta1=stats.beta; pval1=stats.hac.pval; rsq1=stats.adjrsquare; 
stats = regstats2( Dpric(sampfil2), Dyiel(sampfil2),'linear');  beta2=stats.beta; pval2=stats.hac.pval; rsq2=stats.adjrsquare; 
stats = regstats2( Dpric(sampfil3), Dyiel(sampfil3),'linear');  beta3=stats.beta; pval3=stats.hac.pval; rsq3=stats.adjrsquare; 
stats = regstats2( Dpric(sampfil4), Dyiel(sampfil4),'linear');  beta4=stats.beta; pval4=stats.hac.pval; rsq4=stats.adjrsquare; 
stats = regstats2( Dpric(sampfil5), Dyiel(sampfil5),'linear');  beta5=stats.beta; pval5=stats.hac.pval; rsq5=stats.adjrsquare; 

P5slop = [beta1(2) beta2(2) beta3(2) beta4(2) beta5(2)]';
P5sig{1} = staf(pval1(2));
P5sig{2} = staf(pval2(2));
P5sig{3} = staf(pval3(2));
P5sig{4} = staf(pval4(2));
P5sig{5} = staf(pval5(2));
P5rsq = [ rsq1 rsq2 rsq3 rsq4 rsq5]';

% Regression with interactive terms
% Did not use this in the paper. This assumes a linear relation -- the portfolio sorts are less
% restrictive (nonparameteric).

stats = regstats2( Dpric, [Dyiel yiel(2:end) yiel(2:end).*Dyiel]  ,'linear');

beta4=stats.beta; pval4=stats.hac.pval; rsq4=stats.adjrsquare; 

Coeff_Pval_Int =[beta4 pval4 ; rsq4 nan];


end

